package com.example.commons.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.commons.redis.RedisUtil;
import com.example.commons.vo.TestVo;
import com.example.commons.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Author:汪彬彬
 * date:2024/11/8 14:38
 */
public class BeanUtil {

   @Autowired

    /**
     * bean 转 JSON串
     * @return
     */
    public static String bean2JsonString(Object data){
         return JSON.toJSONString(data);
       }

    /**
     * bean转JSON
     * @param data
     * @return
     */
    public static JSONObject bean2Json(Object data){
        return JSON.parseObject(bean2JsonString(data));
    }

    /**
     * JSON 转 map
     * @param
     */

    public static Map<String,Object>  json2Map(JSONObject json){
        Map<String,Object> map = new HashMap<>();
         for(String key :json.keySet()) {
             map.put(key,json.get(key));
         }
        return map;
    }

    /**
     * 对象转map
     * @param data
     * @return
     */
    public static Map<String,Object>  bean2Map(Object data){
        return json2Map(bean2Json(data));
    }


    public  static<T> T obj2Bean(String jsonString,Class<T> clazz){
        JSONObject jsonObject=JSONObject.parseObject(jsonString);
        return JSONObject.toJavaObject(jsonObject,clazz);
    }
    public static void main(String[] args) {
        UserVo userVo = new UserVo();
        userVo.setId(1l);
        List<TestVo> testVos = new ArrayList<>();
        TestVo testVo = new TestVo();
        testVo.setTestName("test");
        userVo.setUsername("test");
        testVos.add(testVo);
        userVo.setList(testVos);
        System.out.println(bean2Map(userVo));
    }

}
